<?php
class Template{
	/**
	 * Enter description here...
	 *
	 * @var Mysql
	 */
	private $db;
	
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	public function add( array $value)
	{
		$v = array();
		$v['name'] = $value['name'];
		$v['content'] = $value['content'];
		$v['seq'] = (int)$value['seq'];
		
		$this->db->insert("template", $v);
		$this->updateGroupCache();
	}
	
	public function change($tid, array $value)
	{
		$v = array();
		isset($value['name']) && $v['name'] = $value['name'];
		isset($value['content']) && $v['content'] = $value['content'];
		isset($value['seq']) && $v['seq'] = (int)$value['seq'];

		$this->db->update("template", $v, 'tid=' . $tid);
		$this->updateGroupCache();
	}
	
	public function del($tid)
	{
		$this->db->delete("template", 'tid=' . $tid);
		$this->updateGroupCache();
	}
	
	
	/**
	 * 读取文章列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function listtemplate($nowp, $per_page)
	{
		
		 $totalNum = $this->db->fetchOne ('select count(*) from template ');
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from template order by seq desc,tid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	
	private function updateGroupCache()
	{
		
	}
	
	public function listAllSubject()
	{
		return $this->db->fetchAll("select tid,name from template order by seq desc,tid desc");
	}
	
	public function show($tid)
	{
		return $this->db->fetchRow("select * from template where tid=\"?\"", array($tid));
	}
}